﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Golf.WebUI.App_code;

namespace Golf.WebUI.AdminShow.Admin
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //登录页面打开清空cookie
            CookiesHelper.ClearCookie("_AdminLogin");
            if (!IsPostBack)
            {
                switch (Request.Form["action"])
                {
                    case "logout":
                        logout(Request, true);
                        break;
                }
            }
        }


        /// <summary>
        /// 退出  系统
        /// </summary>
        /// <param name="request">HttpRequest Request</param>
        /// <param name="logout">是否点击登录logou退出true  false 为系统退出  </param>
        public void logout(HttpRequest request, bool Logout)
        {
            int id = 0;
            Object obj = CookiesHelper.GetCookie("_AdminID");
            if (obj == null || obj.ToString() == "")
            {
                return;
            }
            else
            {
                id = int.Parse(obj.ToString());
            }

            Golf.BLL.Golf_Admin bll = new Golf.BLL.Golf_Admin();
            Golf.Model.Golf_Admin model = bll.GetModel(id);
            if (model == null) return;
            model.LastLoginIP = ThumNail.GetIp(request);//获取ip 
            model.LastLoginTime = DateTime.Now;
            model.IsState = true;
            bll.Update(model);
            //登录页面打开清空cookie
            CookiesHelper.ClearCookie("_AdminLogin");
            CookiesHelper.ClearCookie("_AdminID");
            if (Logout)
                Response.Redirect("http://" + HttpContext.Current.Request.Url.Host + "/AdminShow/Admin/Login.aspx");
            //Response.Write("<script>window.location.href='http://" + HttpContext.Current.Request.Url.Host + "/AdminShow/Admin/Login.aspx';</script>");
            //Response.Write("<script>opener.location.reload();</script>");
        }
    }
}
